package gemini.castor.ui.client.page;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.utils.PlaceConstants.LayoutPosition;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class contains all of the UI components of Root screen.
 * @author fhe
 *
 */
public class RootView extends Composite implements RootPresenter.Display {
	
	@Override
	public void visibleContent(boolean visible) {
		pnlContent.setVisible(visible);
	}

    @UiField
    VerticalPanel pnlContent;
    
    @UiField
    VerticalPanel pnlHeader;
    
    @UiField
    VerticalPanel pnlFooter;
    
    @UiField
    VerticalPanel pnlMenu;
    
    @UiField
    VerticalPanel pnlNavigation;
    
    @UiField
    VerticalPanel pnlContext;
    
    private List<Widget> navigationWidgets;
    private List<Widget> contextWidgets;
    private List<Widget> headerWidgets;

    @UiTemplate("RootView.ui.xml")
    interface MyUiBinder extends UiBinder<Panel, RootView> {
    }

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;

	public RootView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}
	
	@Override
	public String getTextTryAlso(){
		return constants.htmTryAlso();
	}
	@Override
	public String getTextRecognition(){
		return constants.htmRecognition();
	}

    @Override
    public void setWidgetAtIndex(final Widget w, LayoutPosition position, int idx, boolean replace) {
    	if (position.equals(LayoutPosition.CONTEXT)) {
    		setWidgetToList(getContextWidgets(), w, idx, replace);
    		setWidgetsToContext();
    	} else if (position.equals(LayoutPosition.NAVIGATION)) {
    		setWidgetToList(getNavigationWidgets(), w, idx, replace);
    		setWidgetsToNavigation();
        } else if (position.equals(LayoutPosition.FOOTER)) {
        	pnlFooter.clear();
        	pnlFooter.add(w);
        } else if (position.equals(LayoutPosition.HEADER)) {
        	setWidgetToList(getHeaderWidgets(), w, idx, replace);
        	setWidgetsToHeader();
        } else if (position.equals(LayoutPosition.MENU)) {
        	pnlMenu.clear();
        	pnlMenu.add(w);
        } else {
        	pnlContent.clear();
        	pnlContent.add(w);
        }
    }
    
    @Override
    public void setWidgetAtLast(final Widget w, LayoutPosition position, boolean replace) {
    	int idx = 0;
    	if (position.equals(LayoutPosition.CONTEXT)) {
    		idx = pnlContext.getWidgetCount();
    	
    	} else if (position.equals(LayoutPosition.NAVIGATION)) {
    		idx = pnlNavigation.getWidgetCount();
        } 
    	
    	setWidgetAtIndex(w, position, idx, replace);
    }
    
    @Override
    public void setWidgetAtFirst(final Widget w, LayoutPosition position, boolean replace) {
    	setWidgetAtIndex(w, position, 0, replace);
    }
    
    private void setWidgetsToNavigation(){
    	pnlNavigation.clear();
    	for (Widget widget : navigationWidgets) {
    		pnlNavigation.add(widget);
		}
    }
    
    private void setWidgetsToContext(){
    	pnlContext.clear();
    	for (Widget widget : contextWidgets) {
    		pnlContext.add(widget);
		}
    }
    
    private void setWidgetsToHeader(){
    	pnlHeader.clear();
    	for (Widget widget : headerWidgets) {
    		pnlHeader.add(widget);
		}
    }
    
	private void setWidgetToList(List<Widget> list, Widget w, int idx, boolean isReplace) {
		if(list.size() == 0){
			list.add(w);
		} else {
			if (isReplace) {
				if (idx >= list.size()) {
					idx = list.size() - 1;
				}
				
				if (list.get(idx) != null) {
					list.get(idx).removeFromParent();
					list.remove(idx);
				}
				
				if (w != null) {
					list.add(idx, w);
				}
			} else {
				if (idx == -1) {
					list.clear();
					list.add(w);
				} else if (idx >= list.size()) {
					list.add(w);
				} else {
					list.add(idx, w);
				}
			}
		}
	}
    
	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public int getWidgetCount(LayoutPosition position) {
		int result = 1;
		
		if (position.equals(LayoutPosition.CONTEXT)) {
			result = pnlContext.getWidgetCount();
        } else if (position.equals(LayoutPosition.FOOTER)) {
        	result = pnlFooter.getWidgetCount();
        } else if (position.equals(LayoutPosition.HEADER)) {
        	result = pnlHeader.getWidgetCount();
        } else if (position.equals(LayoutPosition.MENU)) {
        	result = pnlMenu.getWidgetCount();
        } else if (position.equals(LayoutPosition.NAVIGATION)) {
        	result = pnlNavigation.getWidgetCount();
        }
		
		return result;
	}

	public List<Widget> getNavigationWidgets() {
		if (navigationWidgets == null) {
			navigationWidgets = new ArrayList<Widget>();
		}
		return navigationWidgets;
	}
	
	public List<Widget> getContextWidgets() {
		if (contextWidgets == null) {
			contextWidgets = new ArrayList<Widget>();
		}
		return contextWidgets;
	}

	@Override
	public String getTextPromotion() {
		return constants.nviPromotions();
	}

	public List<Widget> getHeaderWidgets() {
		if (headerWidgets == null) {
			headerWidgets = new ArrayList<Widget>();
		}
		return headerWidgets;
	}
}